<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<jsp:include page="../common/common.jsp"></jsp:include>
<link rel="stylesheet" href="../css/managecommon.css" type="text/css" />
</head>
<body>
	<div id="main01">
		<table id="dg" ></table>  
	</div>
	
	<div id="td">
		<a href="#" id="myadd" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a>
		<a href="#" id="mydel" class="easyui-linkbutton" data-options="iconCls:'icon-remove'">删除</a>
		<a href="#" id="myupdate" class="easyui-linkbutton" data-options="iconCls:'icon-edit'">修改</a>
		<input type="text" name="searchName" id="sea" />
		<a href="#" id="mysearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
	</div>
	
	<!-- 添加的弹出来的 -->
	<div id="dd">
		
		<div id="inner01">
	        
				宿舍名称:<input name="name"  class="easyui-validatebox" value="厚德" data-options="required:true,validType:''"  style="width:170px;height:20px"> 
				  <br/><br/>
				宿舍人数:<input name="counter"  class="easyui-validatebox" value="6" data-options="required:true,validType:''"  style="width:170px;height:20px"> 
					<br/><br/>
				宿舍性别:<input type="radio" name="sex" value="1">男
					  <input type="radio" name="sex" value="0">女 
				  <br/><br/>
				现住人数:<input name="nowNum" class="easyui-validatebox" value="0" data-options="required:true,validType:''"  style="width:170px;height:20px"> 
					<br/><br/>
					
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a id="newadd" href="#" class="easyui-linkbutton" >添加</a>  
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a id="cz" href="#" class="easyui-linkbutton" >重置</a>  
	      </div>
	
	</div>  
	
	<!-- 修改的弹出来的 -->
	<div id="dd2">
		
		<div id="inner02">
	        
	       		 <input type="text" name="id2" hidden="hidden"  />
				
				宿舍名称:<input name="name2" class="easyui-validatebox" data-options="required:true,validType:''"  style="width:170px;height:20px"> 
				  <br/><br/>
				可住人数:<input name="counter2" class="easyui-validatebox"  data-options="required:true,validType:''"  style="width:170px;height:20px"> 
					<br/><br/>
				宿舍性别:<input type="radio" name="sex2" value="1">男
					  <input type="radio" name="sex2" value="0">女 
				  <br/><br/>
				现住人数:<input name="nowNum2" class="easyui-validatebox"  data-options="required:true,validType:''"  style="width:170px;height:20px"> 
					<br/><br/>
					
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a id="newupdate" href="#" class="easyui-linkbutton" >修改</a>  
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a id="cz" href="#" class="easyui-linkbutton" >重置</a>  
	      </div>
	
	</div>  

	


<script type="text/javascript">

	$(function(){
		
		/*数据表*/
		
		$('#dg').datagrid({    
		    url:'${pageContext.request.contextPath}/dormitory/findWithPage',
		    fitColumns:true,
		   	/* singleSelect:true, */  
		    pagination:true,
		    toolbar:'#td',
		    pageNumber:1,
		    pageSize:4,
		    pageList:[4,8,16],
		    columns:[[    
		        {field:'id',title:'id',checkbox:true},    
		        {field:'name',title:'宿舍名称',width:100},    
		        {field:'counter',title:'可住人数',width:100},    
		        {field:'sex',title:'宿舍性别',width:100},    
		        {field:'nowNum',title:'现住人数',width:100},    
		       
		    ]]    
		});  

		/*总菜单 添加按钮*/
		
		$('#myadd').click(function(){
			
			$('#inner01').css("display","block");
			
			$('#dd').dialog({    
			    title: '添加',    
			    width: 520,    
			    height: 410,    
			    closed: false,    
			    cache: false,    
			    modal: true   
			});    
		});

		/*总菜单 添加按钮 弹出来的 提交按钮*/
		$('#newadd').click(function(){
			
			var a= $("input[name='name']").val();
			var b= $("input[name='counter']").val();
			var c=$('input:checked').val();
			var d= $("input[name='nowNum']").val();
			
			if(undefined==c){
				$.messager.alert('提示','请选择性别'); 
				return false;
			}
			
			c= c==1?"男":"女";
			
			$.ajax({
				'type':'post',
				'dateType':'json',
				'url':'${pageContext.request.contextPath}/dormitory/addDormitory',
				'data':{
					'name':a,
					'counter':b,
					'sex':c,
					'nowNum':d,
				},
				'success':function(msg){
					if(msg.code==200){
						
						$.messager.alert('提示','插入成功');    
						
						setTimeout(goMain(), 1000);
					}else if(msg.code==201){
						$.messager.alert('提示',"宿舍已经存在"); 
					
					}else{
						$.messager.alert('提示','插入失败');    
					}
					
				},
			});
			 
		});
		
		/*总菜单删除的按钮 */
		$('#mydel').click(function(){
			var dels='';
			var obj=$('#dg').datagrid('getSelections');
			var a= $(obj).each(function(i,data){
				var aa=data.id;
				if(obj.length-1==i){
					dels=dels+aa;
				}else{
					dels=dels+aa+",";
				}
			});
			$.ajax({
				'type':'post',
				'dateType':'json',
				'url':'${pageContext.request.contextPath}/dormitory/delDormitory',
				'data':{
					'dles':dels,
					
				},
				'success':function(msg){
					if(msg.code==200){
						
						$.messager.alert('提示',"删除了"+msg.count+"条");
						setTimeout(goMain(), 1000);
					}else{
						$.messager.alert('提示',"删除失败");
					}
				},
			});
		});
		
		
		/*总菜单修改的按钮 */
		$('#myupdate').click(function(){
			
			/*只能选中一个*/
			var obj=$('#dg').datagrid('getSelections');
			if(obj.length==1){
				

				$('#inner02').css("display","block");
				/* 取到值 */
				var a= obj[0].id;
				var b= obj[0].name;
				var c= obj[0].counter;
				var d= obj[0].sex;
				var e= obj[0].nowNum;
				
				
				if("男"==d){
					$("input[value='1']").attr("checked", "checked");
				}else{
					$("input[value='0']").attr("checked", "checked");
				}
				
				/* 赋值 */
				$("input[name='id2']").val(a);
				$("input[name='name2']").val(b);
				$("input[name='counter2']").val(c);
				$("input[name='sex2']").val(d);
				$("input[name='nowNum2']").val(e);
			
				$('#dd2').dialog({    
				    title: '修改',    
				    width: 520,    
				    height: 410,    
				    closed: false,    
				    cache: false,    
				    modal: true   
				});    
			}else{
				$.messager.alert('提示','只能修改的一个');    
			}
			
			
			
		});
		
		/*总菜单修改的按钮 弹出来的提交的按钮*/
		$('#newupdate').click(function(){
			
			var a=$("input[name='id2']").val();
			var b=$("input[name='name2']").val()
			var c=$("input[name='counter2']").val();
			var d=$("input[name='sex2']").val();
			var e=$("input[name='nowNum2']").val();
			
			
			var pp=$('input[name=sex2]:checked').val();
			if(undefined==pp){
				$.messager.alert('提示','请选择性别'); 
				return false;
			}
			
			$.ajax({
				'type':'post',
				'dateType':'json',
				'url':'${pageContext.request.contextPath}/dormitory/updateDormitory',
				'data':{
					
					'id':a,
					'name':b,
					'loginNo':c,
					'sex':d,
					'nowNum':e,
					
				},
				'success':function(msg){
					if(msg.code==200){
						$.messager.alert('提示','更新成功'); 
						setTimeout(goMain(), 1000);
					}else if(msg.code==201){
						$.messager.alert('提示',msg.msg); 
					}else{
						$.messager.alert('提示','更新失败'); 
					}
				},
			});
			 
		});
		
		/*查询*/
		$('#mysearch').click(function(){
			var searchName1=$('#sea').val();
			
			$('#dg').datagrid('load', {    
			    'searchName':searchName1,
			});  


		});
		
		
/*结束*/
	});

		
	

	function goMain(){
		window.location.reload();
	}
	
	
</script>
</body>
</html>